package easytasks.logic;

import java.util.Calendar;

import easytasks.data.DataManager;
import easytasks.data.DataSetup;
import easytasks.main.R;
import easytasks.settings.Settings;
import easytasks.ui.MainActivity;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class TaskNotificationManager
{
	private static final int NOTIFICATION_ID = 0;
	private Context context;
	
	public TaskNotificationManager(Context context)
	{
		if (context == null) throw new IllegalArgumentException("'context' must be not null.");	
		this.context = context;
	}
	
	public boolean getSettingsNotificationsEnabled()
	{
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
		return preferences.getBoolean(Settings.ENABLE_NOTIFICATIONS_KEY, Settings.ENABLE_NOTIFICATIONS_DEFAULT_VALUE);
	}
	
	public void updateNotificationAlarm()
	{
		if (getSettingsNotificationsEnabled()) enableNotificationAlarm();
		else disableNotificationAlarm();
	}
	
	public void enableNotificationAlarm()
	{
		Calendar calendar = Calendar.getInstance();
		calendar.clear(Calendar.HOUR);
		calendar.clear(Calendar.HOUR_OF_DAY);
		calendar.clear(Calendar.MINUTE);
		calendar.clear(Calendar.SECOND);
		calendar.clear(Calendar.MILLISECOND);
		calendar.add(Calendar.DAY_OF_MONTH, 1);
			
		Intent notIntent = new Intent(context, TaskNotificationAlarmReceiver.class);
		PendingIntent notPendIntent = PendingIntent.getBroadcast(context, 0, notIntent, PendingIntent.FLAG_UPDATE_CURRENT);
			
		notPendIntent = PendingIntent.getBroadcast(context, 0, notIntent, 0);
		AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		manager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, notPendIntent);
	}
	
	public void disableNotificationAlarm()
	{
		Intent notIntent = new Intent(context, TaskNotificationAlarmReceiver.class);
		PendingIntent notPendIntent = PendingIntent.getBroadcast(context, 0, notIntent, 0);
		
		AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		manager.cancel(notPendIntent);
	}
	
	public void showNotification()
	{
		DataSetup dataInit = new DataSetup(context);
		DataManager dataManager = new DataManager(dataInit, PreferenceManager.getDefaultSharedPreferences(context));
        
		int todayTaskCount = dataManager.getTodayPendingTaskCount();
		int tomorrowTaskCount = dataManager.getTomorrowPendingTaskCount();
		boolean today = (todayTaskCount > 0);
		boolean tomorrow = (tomorrowTaskCount > 0);
				
		if (today || tomorrow)
		{
			String title = context.getString(R.string.notification_title);
			
			NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
			Notification notification = new Notification(R.drawable.ic_launcher, title, System.currentTimeMillis());
			notification.flags = Notification.FLAG_AUTO_CANCEL;
			
			String content = "";			
			if (today) content += context.getText(R.string.notification_today).toString().replace("#0#", String.valueOf(todayTaskCount));
			if (today && tomorrow) content += ", ";
			if (tomorrow) content += context.getText(R.string.notification_tomorrow).toString().replace("#0#", String.valueOf(tomorrowTaskCount));
				
			Intent notIntent = new Intent(context, MainActivity.class);
			PendingIntent notPendIntent = PendingIntent.getActivity(context, 0, notIntent, 0);
				
			notification.setLatestEventInfo(context, title, content, notPendIntent);
			manager.notify(NOTIFICATION_ID, notification);
		}
	}
}
